package com.study.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.data.ReadCellData;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.Map;

@Slf4j
public class FilterNotNullAmountListener extends AnalysisEventListener<MonthData> {
    // 必须有值的月份
    private Integer month;

    // 不能为空的的月份集合
    private Map<String, String> notNullMap;

    // 必须为空的月份集合
    private Map<String, String> notValueMap;



    public FilterNotNullAmountListener() {
    }

    public FilterNotNullAmountListener(Integer month,Map<String, String> notNullMap,Map<String, String> notValueMap) {
        this.month = month;
        this.notNullMap = notNullMap;
        this.notValueMap = notValueMap;
    }

    /**
     * 这个每一条数据解析都会来调用
     * @author hw
     * @date 2023/3/22 10:27:15
     * @param monthData: 当前行对应的实体
     * @param context: 当前行
     */
    @Override
    public void invoke(MonthData monthData, AnalysisContext context) {
        //当前行数据
        Map<Integer, Cell> cellMap = context.readRowHolder().getCellMap();
        //准备遍历单元格
        Iterator<Map.Entry<Integer, Cell>> iterator = cellMap.entrySet().iterator();

        while (iterator.hasNext()) {
            Map.Entry<Integer, Cell> entry = iterator.next();
            ReadCellData cell = (ReadCellData) entry.getValue();//单元格

            Integer columnIndex = cell.getColumnIndex();//获取单元格索引
            String name = monthData.getName();//获取人员姓名
            BigDecimal v = cell.getNumberValue();//单元格的number
            if (columnIndex == month && null == v) {
                log.error("姓名: {},{} 月份必须有值", name, month);
                notNullMap.put(name, month.toString());
            } else {
                if (columnIndex == 0) {
                    //姓名列 不校验
                    continue;
                }
                if (columnIndex == month) {
                    log.error("姓名: {},{} 月份有值, 校验通过", name, month);
                    //校验通过的业务逻辑代码
                    continue;
                }
                if (null != v) {
                    if (notValueMap.containsKey(name)) {
                        String s = notValueMap.get(name);
                        notValueMap.put(name, s + "," + columnIndex);
                    } else {
                        notValueMap.put(name, columnIndex.toString());
                    }
                    //其他月份
                    log.error("姓名: {},{}月份不能有值", monthData.getName(), columnIndex);
                }

            }
        }
    }

    /**
     * 所有数据解析完成了 都会来调用
     * @author hw
     * @date 2023/3/22 10:28:41
     * @param analysisContext:
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("表格已解析完，打印解析结果");
        notNullMap.forEach((k, v) -> {
            log.info("姓名:{}, {}月份必须有值", k, v);
        });

        notValueMap.forEach((k, v) -> {
            log.info("姓名:{}, {}不月份能有值,", k, v);
        });
    }


}
